#lang racket

(define (square x) (* x x))

(define (expmod base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)
         (remainder (square (expmod base (/ exp 2) m))
                    m))
        (else
         (remainder (* base (expmod base (- exp 1) m))
                    m))))

(define (fermat-test n a)
(= (expmod a n n) a))

(define (complete-fermat-test n)
  (define (iter a) 
    (cond ((= a n) true)
          ((fermat-test n a) (iter (+ a 1)))
          (else false)))
  (iter 2))

(complete-fermat-test 561)

(complete-fermat-test 1105)

(complete-fermat-test 1729)

(complete-fermat-test 2465)

(complete-fermat-test 2821)

(complete-fermat-test 6601)

